<template>
  <div class="number-container d-flex justify-content-center align-items-center">
    <!-- 减 1 的按钮 -->
    <button type="button" class="btn btn-light btn-sm" @click="reduceGoodsCount">-</button>
    <!-- 购买的数量 -->
    <span class="number-box">{{ count }}</span>
    <!-- 加 1 的按钮 -->
    <button type="button" class="btn btn-light btn-sm" @click="addGoodsCount">+</button>
  </div>
</template>

<script>
  import bus from "@/components/eventBus.js"
export default {
  props:{
    // 接受goods传来的id
    id:{
      require:true,
      type: Number
    },
    goods_count:{
      default:1,
      type:Number
    },
    count:{
      default:1,
      type:Number
    }
  },
  methods:{
    // goods_count+1 
    addGoodsCount(){
      this.$emit('num-change', this.count + 1)
    },
    // goods_count-1
    reduceGoodsCount(){
      if( this.goods_count === 0) return
      const obj = { id: this.id, value: this.count - 1 }
      bus.$emit('sendCount', obj)
    }
  }
}
</script>

<style lang="less" scoped>
.number-box {
  min-width: 30px;
  text-align: center;
  margin: 0 5px;
  font-size: 12px;
}

.btn-sm {
  width: 30px;
}
</style>
